from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from myappa.models import *
from utils.jwt_token import check_login
# Create your views here.


# 一级菜单管理(查，增)
class MenuView(APIView):
    def get(self,request):
        # 一级菜单
        menus = MenusModel.objects.filter(menu=None)
        temp = []
        for item in menus:
            # 二级菜单
            menu = MenusModel.objects.filter(menu_id=item.id)
            lst1 =[]
            for items in menu:
                lst1.append({
                    'id': str(items.id),
                    'mname': items.mname,
                    'mpath': items.mpath,
                    'sort': items.sort,
                })
            temp.append({
                    'id': str(item.id),
                    'mname': item.mname,
                    'mpath': item.mpath,
                    'sort': item.sort,
                    'lst1':lst1
                })

        return Response({
            'code':200,
            'msg':'获取一级菜单成功',
            'data':temp
        })
    @check_login
    def post(self,request):
        user_id = request.user_id
        user = UserModel.objects.get(id=user_id)
        if user_id != 1 or user.role.juris.pathname!='/post':
            return Response({
                'code':400,
                'msg':'对不起，您没有该权限'
            })
        mname = request.data.get('mname')
        mpath = request.data.get('mpath')
        sort = request.data.get('sort')
        menu = request.data.get('menu')
        if not all([mname,mpath,sort,menu]):
            return Response({
                'code':400,
                'msg':"参数不完整"
            })
        menus = MenusModel.objects.filter(mname=mname).first()
        if not menus:
            MenusModel.objects.create(mname=mname,mpath=mpath,sort=sort,menu=menu)
            return Response({
                'code':201,
                'msg':'添加菜单成功',
            })
        else:
            return Response({
                'code':400,
                'msg':'已有该菜单'
            })


# 菜单管理(查，改，删)
class MenusInfoView(APIView):
    def get(self,request,id):
        menus = MenusModel.objects.get(id=id)
        if not menus:
            return Response({
                'code':400,
                'msg':'没有该菜单'
            })
        return Response({
            'code':200,
            'msg':'获取单个菜单成功',
            'data':{
                'id': menus.id,
                'mname': menus.mname,
                'mpath': menus.mpath,
                'sort': menus.sort
            }
        })

    @check_login
    def put(self,request,id):
        user_id = request.user_id
        user = UserModel.objects.get(id=user_id)
        if user_id != 1 or user.role.juris.pathname != '/put':
            return Response({
                'code': 400,
                'msg': '对不起，您没有该权限'
            })
        mname = request.data.get('mname')
        mpath = request.data.get('mpath')
        sort = request.data.get('sort')
        menu = request.data.get('menu')
        menus = MenusModel.objects.get(id=id)
        if not menus:
            return Response({
                'code':400,
                'msg':"没有该菜单"
            })
        if mname != menus.mname:
            menus.mname = mname
        if mpath != menus.mpath:
            menus.mpath = mpath
        if sort != menus.sort:
            menus.sort = sort
        if menu != menus.menu:
            menus.menu = menu
        menus.save()
        return Response({
            'code':200,
            'msg':'修改成功'
        })
    @check_login
    def delete(self,request,id):
        user_id = request.user_id
        user = UserModel.objects.get(id=user_id)
        if user_id != 1 or user.role.juris.pathname != '/delete':
            return Response({
                'code': 400,
                'msg': '对不起，您没有该权限'
            })
        MenusModel.objects.filter(id=id).delete()
        return Response({
            'code':200,
            'msg':'删除成功'
        })

